#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned count;

void * dec_count(void * arg){
	pthread_mutex_lock(&mutex);
	while (count <=0 )
		pthread_cond_wait(&cond, &mutex);
	/* do something that requires holding the mutex and condition is true */
	count --;
	printf("dec count=%d\n",count);
	pthread_mutex_unlock(&mutex);
}

void * inc_count(void * arg){
	pthread_mutex_lock(&mutex);
	/* do something that might make condition true */
		count ++;
	printf("inc count=%d\n",count);
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
}


void main(int argc, char ** argv){
	pthread_t tidA;
	pthread_t tidB;

	printf("hello\n");
	int result_code;

	result_code = pthread_create(&tidB, NULL, dec_count, (void *)0);
	if(result_code){
		printf("create thread error\n");
		exit(1);
	}

	result_code = pthread_create(&tidA, NULL, inc_count, (void *)0);
	if(result_code){
		printf("create thread error\n");
		exit(1);
	}
	pthread_join(tidA, NULL);
	pthread_join(tidB, NULL);

	printf("all thread done\n");
}